package com.amoby.base.commbank.reportfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

import com.amoby.base.utils.DateUtils;

/**
 * 用来解析CSV type 1格式的事务处理事件信息
 * 
 */
public class TGEventsCSV1Resolver extends FileResolver {
    public ReportEvents resolver(File file) throws IOException {
        TransactionGroupEvents bean = new TransactionGroupEvents();
        bean.setFileName(file.getName());
        this.reportEvents = bean;
        InputStreamReader in = null;
        CSVReader reader = null;
        try {
            in = new InputStreamReader(new FileInputStream(file));
            reader = new CSVReader(in);
            List<String[]> listRows = reader.readAll();
            String recordType = null;
            String dateStr = null;
            for (String[] cells : listRows) {
                recordType = cells[0];
                if ("01".equals(recordType)) {
                    bean.setReportTitle(cells[1]);
                    bean.setOriginalFilename(cells[2]);
                    dateStr = cells[3].trim() + " " + cells[4].trim();
                    bean.setFileDate(DateUtils.parse(dateStr, "dd/MM/yy HH:mm"));
                    bean.setClientName(cells[5]);
                    bean.setStatus(cells[6].trim());
                } else if ("02".equals(recordType)) {
                    bean.setInstructionId(cells[1]);
                    bean.setClientId(cells[2]);
                    bean.setProcessingDate(DateUtils.parse(cells[3], "dd/MM/yy"));
                    bean.setTotalAmount(Double.valueOf(cells[4].trim()));
                } else if ("03".equals(recordType)) {
                    bean.setErrCode(cells[1]);
                    bean.setSeverityLevel(cells[2]);
                    bean.setErrMsg(cells[3]);
                } else if ("99".equals(recordType)) {
                } else {
                }
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (reader != null) {
                reader.close();
                reader = null;
            }
            if (in != null) {
                in.close();
                in = null;
            }
        }
        return this.reportEvents;
    }
}
